package com.ruoyi.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.hospital.domain.Departments;
import com.ruoyi.hospital.domain.Hospitals;
import com.ruoyi.hospital.mapper.DepartmentsMapper;
import com.ruoyi.hospital.service.DepartmentsService;
import com.ruoyi.hospital.service.HospitalsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentsServiceImpl extends ServiceImpl<DepartmentsMapper, Departments> implements DepartmentsService {

    @Autowired
    private HospitalsService hospitalsService;
    @Autowired
    private DepartmentsMapper departmentsMapper;

    @Override
    public List<Departments> selectDepartmentsList(Departments departments) {
        return departmentsMapper.selectDepartmentsList(departments);
    }

    // 重构方法名称以更清晰表达功能
    private List<Departments> setHospitalNames(List<Departments> departmentsList) {
        List<Departments> resultList = new ArrayList<>();

        if (departmentsList == null || departmentsList.isEmpty()) {
            return resultList;
        }

        departmentsList.forEach(department -> {
            try {
                // 获取医院ID并检查是否为空
                String hospitalId = department.getHospitalId();
                if (StringUtils.hasText(hospitalId)) {
                    // 查询医院信息并设置医院名称
                    Hospitals hospital = hospitalsService.getById(hospitalId);
                    if (hospital != null) {
                        department.setHospitalName(hospital.getHospitalName());
                    }
                }
                resultList.add(department);
            } catch (Exception e) {
                resultList.add(department); // 仍然添加部门信息，只是医院名称可能为空
            }
        });

        return resultList;
    }

    @Override
    public Departments selectDepartmentsByDeptId(Long deptId) {
        // 使用 LambdaQueryWrapper 替代 QueryWrapper
        return super.getOne(new LambdaQueryWrapper<Departments>().eq(Departments::getDeptId, deptId));
    }

    @Override
    public boolean insertDepartments(Departments departments) {
        // 添加空值检查
        return departments != null && super.save(departments);
    }

    @Override
    public boolean updateDepartments(Departments departments) {
        // 添加空值检查和ID有效性检查
        if (departments == null || departments.getDeptId() == null) {
            return false;
        }

        // 使用 LambdaQueryWrapper 替代 QueryWrapper
        LambdaQueryWrapper<Departments> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Departments::getDeptId, departments.getDeptId());

        // 执行更新操作
        return super.update(departments, queryWrapper);
    }

    @Override
    public boolean deleteDepartmentsByDeptIds(List<Long> deptIds) {
        // 添加空值检查和集合有效性检查
        if (deptIds == null || deptIds.isEmpty()) {
            return false;
        }

        // 使用 LambdaQueryWrapper 替代 QueryWrapper
        LambdaQueryWrapper<Departments> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Departments::getDeptId, deptIds);

        return super.remove(queryWrapper);
    }

    @Override
    public List<Departments> selectDeptByHospitalId(Departments query) {
        return departmentsMapper.selectDeptByHospitalId(query);
    }
}